public class No022 {
    /**
     * 输入一个链表，输出该链表中倒数第k个结点。
     */
    public class ListNode {
        int val;
        ListNode next = null;

        ListNode(int val) {
            this.val = val;
        }
    }

    //p1先走k步，p1走到末尾时p2走到倒数k个节点
    public ListNode FindKthToTail(ListNode head, int k) {
        if (head == null || k == 0) return null;
        ListNode p1 = new ListNode(0);
        p1.next = head;
        for (int i = 0; i < k ; i++) {
            if (p1.next !=null){
                p1 = p1.next;
            }else return null;
        }
        ListNode p2 = new ListNode(0);
        p2.next = head;
        while (p1 !=null){
            p1 = p1.next;
            p2 = p2.next;
        }
        return p2;
    }
}
